<?php

namespace Home\Controller;

use Think\Controller;

/**
 * 外包客服外呼中心
 */
class TempController extends CommnController
{
    public function __construct()
    {
        parent::__construct();
    }

    //渠道用户列表 待查
    public function channelList()
    {
//        $where['ctime']=array('bettwen',1511452800,1511625600);
//        $where['channel']=array('eq','ch_42384057');
////        $data=M('user','','USER')->join('a join dxd_caiwu.user_borrow on ')->where($where)->field('vid,ctime')->select();
//        $data=M('user','','USER')->query('SELECT a.vid,a.ctime,c.logic_level,a.sex
//                                          FROM dxd_user.user a left join dxd_caiwu.user_borrow b on a.vid=b.vid
//                                          left join dxd_user.user_good_bad c on a.vid=c.vid
//                                          LEFT JOIN dxd_kefu.tmp_user d on a.vid=d.vid
//                                          WHERE a.channel=\'ch_42384057\' AND b.vid is NULL AND d.vid is null AND a.ctime>1511452800 and a.ctime<1511625600;');

        if ($_SESSION['role_id'] != '3' && $_SESSION['role_id'] != '1') {
            $where['b.out_kf_id'] = array('eq', $_SESSION['id']);
        }

        $search = I('get.user');
        $where['a.mobile|a.truename'] = array('like', '%' . $search . '%');
        $where['b.status'] = array('in', array('0', '1'));
        $tol = M('tmp_channel_user', '', 'KEFU')->join('b join dxd_user.user a on b.vid=a.vid')->where($where)->distinct('a.vid')->count();
        $row = '10';
        $page = new PageController($tol, $row);
        $fpage = $page->fpage();
        $data = M('tmp_channel_user', '', 'KEFU')->join('b join dxd_user.user a on b.vid=a.vid')->where($where)->field('b.vid,b.out_kf_id,b.status,b.logic_level as score,a.truename as name,a.mobile,a.sex,a.ctime as time')->LIMIT($page->listfirst, $page->listRows)->select();

        foreach ($data as $k => $v) {
            $data[$k]['time'] = date('Y-m-d H:i', $v['time']);
            if ($v['status'] == 1) {
                $data[$k]['status'] = '正在调查';
            } else if ($v['status'] == 0) {
                $data[$k]['status'] = '未调查';
            }
            if ($v['sex'] == 1) {
                $data[$k]['sex'] = '男';
            } else if ($v['sex'] == 2) {
                $data[$k]['sex'] = '女';
            } else {
                $data[$k]['sex'] = '未知';
            }
            $data[$k]['admin_name'] = M('admin', '', 'KEFU')->where(['id' => $v['out_kf_id']])->getfield('name');
        }
//        var_dump($data);die;

        $this->assign(array(
            'user' => $data,
            'fpage' => $fpage,
//            'url_type' => $url_type,
            'page_first' => $page->listfirst,
            'tol' => $tol
        ));
        $this->page = I('page');
        $this->display();
    }

 //渠道用户列表 已经调查
    public function alreadyCallList()
    {
        if ($_SESSION['role_id'] != '3' && $_SESSION['role_id'] != '11') {
            $where['b.out_kf_id'] = array('eq', $_SESSION['id']);
        }

//        $where['ctime']=array('bettwen',1511452800,1511625600);
//        $where['channel']=array('eq','ch_42384057');
////        $data=M('user','','USER')->join('a join dxd_caiwu.user_borrow on ')->where($where)->field('vid,ctime')->select();
//        $data=M('user','','USER')->query('SELECT a.vid,a.ctime,c.logic_level,a.sex
//                                          FROM dxd_user.user a left join dxd_caiwu.user_borrow b on a.vid=b.vid
//                                          left join dxd_user.user_good_bad c on a.vid=c.vid
//                                          LEFT JOIN dxd_kefu.tmp_user d on a.vid=d.vid
//                                          WHERE a.channel=\'ch_42384057\' AND b.vid is NULL AND d.vid is null AND a.ctime>1511452800 and a.ctime<1511625600;');

        $search = I('get.user');
        $where['a.mobile|a.truename'] = array('like', '%' . $search . '%');
        $where['b.status'] = array('eq',2);
        $tol = M('tmp_channel_user', '', 'KEFU')->join('b join dxd_user.user a on b.vid=a.vid')->where($where)->distinct('a.vid')->count();
        $row = '10';
        $page = new PageController($tol, $row);
        $fpage = $page->fpage();
        $data = M('tmp_channel_user', '', 'KEFU')->join('b join dxd_user.user a on b.vid=a.vid')->where($where)->field('b.vid,b.out_kf_id,b.status,b.logic_level as score,a.truename as name,a.mobile,a.sex,a.ctime as time,a.cert_status')->LIMIT($page->listfirst, $page->listRows)->select();

        foreach ($data as $k => $v) {
            $data[$k]['time'] = date('Y-m-d H:i', $v['time']);
            if ($v['cert_status'] == 1) {
                $data[$k]['cert_status'] = '未认证';
            } else if ($v['cert_status'] == 2) {
                $data[$k]['cert_status'] = '已通过';
            }else if ($v['cert_status'] == 3) {
                $data[$k]['cert_status'] = '未通过';
            }else if ($v['cert_status'] == 3) {
                $data[$k]['cert_status'] = '审核中';
            }

            if ($v['sex'] == 1) {
                $data[$k]['sex'] = '男';
            } else if ($v['sex'] == 2) {
                $data[$k]['sex'] = '女';
            } else {
                $data[$k]['sex'] = '未知';
            }
            $data[$k]['admin_name'] = M('admin', '', 'KEFU')->where(['id' => $v['out_kf_id']])->getfield('name');
        }
//        var_dump($data);die;

        $this->assign(array(
            'user' => $data,
            'fpage' => $fpage,
//            'url_type' => $url_type,
            'page_first' => $page->listfirst,
            'tol' => $tol
        ));
        $this->page = I('page');
        $this->display();
    }


    /**
     * 添加快速回复标签
     */
    public function Quick_answer()
    {
        if (empty($_POST)) {
            $vid = I('get.vid');
            $url_type = I('get.url_type');
            $data = M('kefu_quick_answer', '', 'KEFU')->where(['type' => '1'])->field('title,id')->select();
            $this->assign(array(
                'data' => $data,
                'vid' => $vid,
                'url_type' => $url_type
            ));
            $this->display();
            die;
        }
        $title = I('title');
        $arr = array('title' => $title, 'ctime' => time(), 'kf_id' => $_SESSION['id'], 'type' => 1);
        $add = M('kefu_quick_answer', '', 'KEFU')->add($arr);
        if ($add) {
            add_log('添加快速回复' . $add);
            $this->ajax('1001', '添加成功');
            die;
        }
        $this->ajax('1002', '添加失败');
        die;
    }

    /**
     * 回复记录
     */
    public function Remarks()
    {
        if (empty($_POST)) {
            $vid = I('get.vid');
            $url_type = I('get.url_type');
            $this->assign(array(
                'vid' => $vid,
                'url_type' => $url_type
            ));
            $this->display();
            die;
        }
    }


    /**
     * 回复记录
     */
    public function outbound_result()
    {

        $vid = I('vid');
        $type1 = I('type1');
        $content = I('content');
        $quick_status = I('quick_status');
        $arr = array('admin_name' => $_SESSION['name'], 'content' => $content, 'ctime' => time(), 'vid' => $vid);
        $this->page = I('page');
        $add = M('tmp_channel_mark', '', 'KEFU')->add($arr);
        if ($add) {

            $arr = array('status' => 2);
            M('tmp_channel_user', '', 'KEFU')->where(['vid' => $vid])->save($arr);

            add_log('对用户' . $vid . '外呼备注ID' . $add);
            $this->ajax('1001', '备注成功');
            die;
        }
        $this->ajax('1002', '备注失败');
        die;
    }


    /**
     * 待审核审核列表
     */
    public function fk_check()
    {
        $url_type = I('get.type');//1 风控未通过用户列表 2 无机型注册未认证 3 通过未借款  4 有机型注册未认证
        $this->url_type_status = I('get.type_status');//0 全部 1没沟通 2 沟通过 3 已经解决 4 去认证  用户列表沟通状态
        $page_first = I('get.page_first');
        $search = I('get.user');
        $this->rid = I('rid');
        $this->roles = role(I('rid'));
        $this->page = I('page', 0);
        $this->user_content = $search;
        $url_type_status = $this->url_type_status;

        $where['a.mobile|a.truename'] = array('like', '%' . $search . '%');
        $where['b.status'] = array('in', array('2', '3'));
        if ($_SESSION['role_id'] != '3' && $_SESSION['role_id'] != '11') {
            $where['b.out_kf_id'] = array('eq', $_SESSION['id']);
        }

        $sum = M('anti_check_user', '', 'KEFU')->join('b join dxd_user.user a on b.vid=a.vid')->where($where)->distinct('a.vid')->field('a.vid')->select();
        $tol = count($sum);
        $row = '10';
        $page = new PageController($tol, $row);
        $fpage = $page->fpage();
        if (!empty($page_first)) {
            $page->listfirst = $this->page;
        }

        $field = 'a.truename as name,a.mobile,a.sex,b.status,b.commit_time,a.vid';
        $user = M('anti_check_user', '', 'KEFU')->join('b join dxd_user.user a on b.vid=a.vid')->where($where)->LIMIT($page->listfirst, $page->listRows)->order('b.commit_time desc')->field($field)->select();

        foreach ($user as $k => $v) {
            //用户评分
            $user[$k]['logic_score'] = M('user_good_bad', '', 'USER')->where(['vid' => $v['vid']])->getfield('logic_level');


            if ($v['sex'] == 1) {
                $user[$k]['sex'] = '男';
            } else if ($v['sex'] == 2) {
                $user[$k]['sex'] = '女';
            } else {
                $user[$k]['sex'] = '未知';
            }
            if ($v['commit_time'] != 0) {
                $user[$k]['commit_time'] = date('Y-m-d H:i', $v['commit_time']);
            } else {
                $user[$k]['commit_time'] = '-';
            }

            if ($v['status'] == '2') {
                $user[$k]['status'] = '待接收';
                $user[$k]['check_status'] = '1';

            } elseif ($v['status'] == '3') {
                $user[$k]['status'] = '审核中';
                $user[$k]['check_status'] = '2';
            }

        }
        $this->assign(array(
            'user' => $user,
            'fpage' => $fpage,
//            'url_type' => $url_type,
            'page_first' => $page->listfirst,
            'tol' => $tol
        ));
        $this->page = I('page');

        $this->display();
    }



    /**
     * 用户详情
     */
    public function user_info()
    {

        $page11 = I('page11', 0);
        $page_first = I('get.page_first11');
        $vid = I('get.vid');

        //修改调查状态
        M('tmp_channel_user', '', 'KEFU')->where(['vid' => $vid])->setfield('status',1);

        $user = M('user', '', 'USER')->where(['vid' => $vid])->field('vid,mobile,sex,ctime,address,residence_time,id_card')->find();

        $first = substr($user['id_card'], 0, 3);
        $end = substr($user['id_card'], strlen($user['id_card']) - 9, strlen($user['id_card']));
        $user['id_card'] = $first . '******' . $end;

        $user_pic = M('user_idcard_img', '', 'USER')->where(['vid' => $user['vid']])->field('photo,photo1,photo2,front,behind')->find();
        $user['ctime'] = date('Y-m-d H:i', $user['ctime']);
        $user_contact = M('user_contact', '', 'USER')->where(['vid' => $vid])->field('relative_name,relative_mobile,r_type,other_name,other_mobile,f_type')->find();
        $w = [];
        $w['mymobile'] = array('eq', $user['mobile']);
        $w['call_other_number'] = array('eq', $user_contact['relative_mobile']);

        $contact_sum = M('mobile_callrecord_s', '', 'KF_USER_TX')->where($w)->count();
        $contact_sum2 = M('mobile_callrecord_s', '', 'KF_USER_TX')->where(['mymobile' => $user['mobile'], 'call_other_number' => $user_contact['other_mobile']])->count();
        $user_contact['contact_sum'] = $contact_sum;
        $user_contact['contact_sum2'] = $contact_sum2;

        $user['score'] = M('user_good_bad', '', 'USER')->where(['vid' => $vid])->getfield('logic_level');

//        $user['score'] = M('anti_cheat', '', 'KEFU_FK')->where(['vid' => $vid])->getfield('logic_score');

        //通讯录列表
        $field = "b.call_other_number,count(b.call_other_number) as num, a.mobile";
        $tol = M('user_mobilebook', '', 'KF_USER_TX')->where(['vid' => $vid])->count();
        $row = '10';
        $page = new PageController($tol, $row);
        $fpage = $page->fpage();

//       $user_tx = M('user_mobilebook', '', 'KF_USER_TX')->join('a left join zhengxin.mobile_callrecord_s b on a.mobile=b.call_other_number')->where(['a.vid' => $vid,'b.mymobile'=>$user['mobile']])->field($field)->LIMIT($page->listfirst, $page->listRows)->group('b.call_other_number')->order('num desc')->select();
        $user_tx = M('user_mobilebook', '', 'KF_USER_TX')->query('SELECT a.name , a.mobile as call_other_number ,IFNULL(b.num,0) as num
                                                              from user_mobilebook a 
                                                              left join (SELECT call_other_number,COUNT(call_other_number) as num 
                                                                          FROM mobile_callrecord_s 
                                                                          WHERE mymobile=\'' . $user['mobile'] . '\' 
                                                                          GROUP BY call_other_number) b 
                                                                          on a.mobile=b.call_other_number 
                                                                          WHERE  a.vid=\'' . $user['vid'] . '\'
                                                                          ORDER BY num desc LIMIT ' . $page->listfirst . ',' . $page->listRows);
        $this->assign(array(
            'user' => $user,
            'user_tx' => $user_tx,
            'user_pic' => $user_pic,
            'user_contact' => $user_contact,
            'fpage' => $fpage,
            'tol' => $tol,
            'pages' => $page->listfirst,
            'vid' => $vid,
            'page11' => $page11,
            'page_first11' => $page_first
        ));


        $this->display();
    }





    /**
     * 编辑内容
     */
    public function edit_content()
    {
        if (empty($_POST)) {
            $id = I('get.id');
            $content = M('tmp_channel_mark', '', 'KEFU')->where(['id' => $id])->field('content,vid')->find();
            $this->assign(array(
                'content' => $content['content'],
                'id' => $id,
                'vid' => $content['vid']
            ));
            $this->display();
        } else {
            $id = I('id');
            $content = I('edit_content');

            $info = M('tmp_channel_mark', '', 'KEFU')->where(['id' => $id])->setfield('content', $content);
            if ($info) {
                add_log('修改外呼记录' . $id);
                $this->ajax('1001', '修改成功');
                die;
            }
            $this->ajax('1002', '修改失败');
            die;
        }

    }


    /**
     * 详情
     */

    public function list_details()
    {
        $vid = I('get.vid');
        $data = M('tmp_channel_mark', '', 'KEFU')->join('b join dxd_user.user a on b.vid=a.vid')->where(['b.vid' => $vid])->field('b.id,b.admin_name,b.content,b.ctime,b.vid,a.truename,a.mobile')->select();

        foreach ($data as $k => $v) {
            $data[$k]['ctime'] = date('Y-m-d H:i', $v['ctime']);
        }

//        $this->admin_name=$_SESSION['name'];
//        $this->id=$id;
        $url_type = I('url_type');
        $type_status = I('type_status');
        $this->page = I('page');
        $this->assign(array(
            'data' => $data,
            'vid' => $vid,
            'url_type' => $url_type,
            'type_status' => $type_status
        ));

        $this->display();
    }


    /**
     * 发送post请求
     * @param string $url 请求地址
     * @param array $post_data post键值对数据
     * @return string
     */
    public function send_post($url, $post_data)
    {

        $postdata = http_build_query($post_data);
        $options = array(
            'http' => array(
                'method' => 'POST',
                'header' => 'Content-type:application/x-www-form-urlencoded',
                'content' => $postdata,
                'timeout' => 15 * 60 // 超时时间（单位:s）
            )
        );
        $context = stream_context_create($options);
        $result = file_get_contents($url, false, $context);
        return $result;
    }

    // 今天还款用户

    public function today_pay()
    {
        $where=[];
        $where['a.status']=array('neq',3);
//        $where['a.repay_time']=array(array('gt',1513180800),array('lt',1513267200),'and');
        $start=strtotime(date('Y-m-d',time()));
        $end=strtotime(date('Y-m-d 23:59:59',time()));
        $where['a.repay_time']=array('BETWEEN',array($start,$end));

        $sum =M('user_repayment','','CAIWU')->join('a join dxd_user.user b on a.vid=b.vid')->where($where)->distinct('a.vid')->field('a.vid')->select();
        $tol = count($sum);
        $row = '10';
        $page = new PageController($tol, $row);
        $fpage = $page->fpage();

         $field='a.repay_money,a.type,a.status,a.repay_time,b.mobile,b.truename,b.vid,b.sex';
         $data=M('user_repayment','','CAIWU')->join('a join dxd_user.user b on a.vid=b.vid')->field($field)->where($where)->LIMIT($page->listfirst, $page->listRows)->select();

//         var_dump($data);die;
         foreach ($data as $k=>$v){
             if($v['status']==1){
                 $data[$k]['status']='待还款';
             } else    if($v['status']==2){
                 $data[$k]['status']='还款中';
             } else    if($v['status']==3){
                 $data[$k]['status']='还款成功';
             } else    if($v['status']==4){
                 $data[$k]['status']='还款失败';
             } else    if($v['status']==5){
                 $data[$k]['status']='取消还款';
             }

             if($v['sex']==1){
                 $data[$k]['sex']='男';
             }else if($v['sex']==2){
                 $data[$k]['sex']='女';
             }else{
                 $data[$k]['sex']='未知';
             }

              $data[$k]['repay_time']=date('Y-m-d H:i',$v['repay_time']);
              $w=[];
              $w['vid']=$v['vid'];
              $w['ctime']=array('gt',strtotime(date('Y-m-d',time())));
             $called=M('temp_today_pay_mark', '', 'KEFU')->where($w)->find();
             if(!empty($called)){
                 $data[$k]['is_call']='已拨打';
             }else{
                 $data[$k]['is_call']='未拨打';
             }
         }
         $show_phonebar=M('admin','','KEFU')->where(['id'=>$_SESSION['id']])->field('status,agent_status')->find();
        $show=2;
        if ($show_phonebar['status']==1 &&$show_phonebar['agent_status']==1 ){
            $show=1;
        }
        $this->assign(array(
            'data' => $data,
            'fpage' => $fpage,
//            'url_type' => $url_type,
            'page_first' => $page->listfirst,
            'tol' => $tol,
            'show' => $show
        ));
//        $this->page = I('page');
        $this->display();
    }
    //备注
    public function mark()
    {
        $vid=I('get.vid');
        $list=M('temp_today_pay_mark', '', 'KEFU')->where(['vid'=>$vid])->field('vid,reason,name,ctime')->select();
        foreach ($list as  $k=>$v){
            if($v['ctime']>100){
                $list[$k]['ctime']=date('Y-m-d H:i',$v['ctime']);
            }else{
                $list[$k]['ctime']='-';
            }
        }
        $this->assign(array(
            'data' => $list,
            'vid'=>$vid,
//            'tol' => $tol
        ));
        $this->display();

    }

    /*
     * 弹框添加备注
     * */
    public function add_mark(){
        $mark= I('info');
        $vid=I('vid');
        if(empty(trim($mark))){
            $this->ajax('1002', '信息填写完整！');
        }
        $arr=array('vid'=>$vid,'reason'=>$mark,'name'=>$_SESSION['name'],'ctime'=>time());
        if(M('temp_today_pay_mark', '', 'KEFU')->add($arr)){
            $this->ajax('1001', '备注成功！');
        }else{
            $this->ajax('1002', '备注失败！');
        }

    }
}